//设置，改， 删除
function setCookie(cname, cvalue, exdays) {
    var d = new Date();

    d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);

    var expires = "expires=" + d.toUTCString();

    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";

}

// getCookie("username")
function getCookie(cname) {
    //Hm_lvt_f801name=admin
    //passw77df4b25b2662225b26a02a32ab8=1577930187
    //     //userord=123456999

    var name = cname + "="; // username=
    var ca = document.cookie.split(';');
    var len = ca.length;
    for (var i = 0; i < len; i++) {
        //username=admin
        var c = ca[i].trim();

        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }

    }

    return '';

}

//操作内容， 兼容的方法
function inText(obj, value) {
    //IE或非IE高版本
    if (document.all) {
        if (typeof (value) == "undefined") {
            return obj.innerText;
        } else {
            obj.innerText = value;
        }
    } else {
        if (typeof (value) == "undefined") {
            return obj.textContent;
        } else {
            obj.textContent = value;
        }
    }
}

//通过URL获取文件
//http://localhost:63342/01/img/on.png
function getUrlName(url) {
    var l = url.lastIndexOf("/") + 1;

    var filename = url.substring(l);

    return filename;

}

/*
*  添加类样式 addClass()
*  element 必须有的参数， 设置类样式的元素
*  value  类名， 元素添加的类名， 如果这个元素已经设置这个类了， 就不再设置， 如果没有设置过任何类， 就新加一个， 如果，
*
* */

function addClass(element, value) {
    if (!element.className) {
        element.className = value;
    } else {
        if (element.className.indexOf(value) == -1) {
            var src = element.className;

            src += " ";
            src += value;

            element.className = src;
        }
    }
}

/*
    删除类样式 delClass(element, value)
    如果没有提供第二个参数，就删除所有的样式
    如果元素中只有一个类样式， 直接删除一个就可以了

    如果删除类中的一个成员

 */

function delClass(element, value) {
    // one == one
    if (typeof (value) == "undefined" || element.className == value) {
        element.className = '';
    } else {
        // className = "one two three";
        // classNames = [one, two, three]
        var classNames = element.className.split(' ');
        var str = "";
        for (var i = 0; i < classNames.length; i++) {

            if (classNames[i] != value) {
                str += classNames[i];
                str += " ";
            }
        }

        str = str.trim();

        element.className = str;

    }
}


//通过类名获取所有元素的方法
function gclass(className, tagName) {
    // var alltags = document.all;
    var alltags = document.getElementsByTagName(tagName || "*");
    var arr = new Array();

    for (let i = 0; i < alltags.length; i++) {
        // one two
        var one = className.split(" ");
        var two = alltags[i].className.split(" ");

        if (isHasArray(one, two)) {
            arr[arr.length] = alltags[i];
        }
    }

    //返回所有类元素集合
    return arr;
}

// 判断一个数组中的所有元素是否在另外的一个数组中存在
function isHasArray(one, two) {


    if (two.length < one.length) {
        return false;
    }
    var flag = 0;
    for (let i = 0; i < one.length; i++) {
        for (let j = 0; j < two.length; j++) {
            if (one[i] == two[j]) {
                flag++;
                break;
            }
        }
    }

    return flag == one.length;

}

//获取子节点

function childs(element) {
    var nodes = element.childNodes;
    var arr = new Array();
    for (let i = 0; i < nodes.length; i++) {
        if (nodes[i].nodeType == 1) {
            arr[arr.length] = nodes[i];//arr.push(nodes[i]
        }
    }

    return arr;

}

// 获取第一个子节点
function cfrist(element) {
    var node = element.firstChild;

    while (node && node.nodeType != 1) {
        node = node.nextSibling;
    }

    return node;
}

// 获取最后一个子节点
function clast(element) {
    var node = element.lastChild;

    while (node && node.nodeType != 1) {
        node = node.previousSibling;
    }

    return node;
}

// 获取下一个兄弟节点
function cnext(element) {
    var node = element.nextSibling;

    while (node && node.nodeType != 1) {
        node = node.nextSibling;
    }

    return node;
}

// 获取上一个兄弟节点
function cprev(element) {
    var node = element.previousSibling;

    while (node && node.nodeType != 1) {
        node = node.previousSibling;
    }

    return node;
}

//获取当前节点后面所有的元素
function nextAll(element, tagName) {
    var r = [];
    for (var n = element.nextSibling; n; n = n.nextSibling) {
        if (n.nodeType == 1) {
            if (typeof (tagName) != "undefined") {
                if (tagName.toUpperCase() == n.nodeName) {
                    r.push(n);
                }
            } else {
                r.push(n);
            }

        }
    }
    return r;
}

//获取当前节点前面所有的元素
function prevAll(element, tagName) {
    var r = [];
    for (var n = element.previousSibling; n; n = n.previousSibling) {
        if (n.nodeType == 1) {
            if (typeof (tagName) != "undefined") {
                if (tagName.toUpperCase() == n.nodeName) {
                    r.push(n);
                }
            } else {
                r.push(n);
            }

        }
    }
    return r;
}

//获取所有同胞节点
function siblings(element, tagName) {
    return prevAll(element, tagName).concat(nextAll(element, tagName));
}

// 获取定位元素到页面顶点的left
function offsetLeftAll(obj) {
    var left = obj.offsetLeft;

    var po = obj.offsetParent;

    while (po != null) {
        left += po.offsetLeft;
        po = po.offsetParent;

    }


    return left;

}

// 获取定位元素到页面顶点的Top
function offsetTopAll(obj) {
    var top = obj.offsetTop;

    var po = obj.offsetParent;

    while (po != null) {
        top += po.offsetTop;
        po = po.offsetParent;

    }


    return top;
}

//格式化Date类
Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "h+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };

    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(
                RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }

    return fmt;
    /*alert(new Date().format("yyyy年MM月dd日"));
    alert(new Date().format("MM/dd/yyyy"));
    alert(new Date().format("yyyyMMdd"));
    alert(new Date().format("yyyy-MM-dd hh:mm:ss"));*/
}

//获取时间格式化
function nowNumTime() {
    var now = new Date();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    var hhh = "" + (hour > 12 ? hour - 12 : hour);
    if (hour === 0) {
        hhh = "12";
    }
    hhh = hhh > 10 ? hhh : "0" + hhh;
    hhh = hhh + (minute > 10 ? ":" + minute : ":0" + minute);
    hhh = hhh + (second > 10 ? ":" + second : ":0" + second);
    hhh = hhh + (hour > 12 ? "  P.M" : "  A.M");
    return hhh;
}

//获取随机数
function random(max, min) {
    return Math.floor(Math.random() * (max - min) + min);
}

//获取rgb颜色rgb(0~225,0~225,0~225);

function randomcolol() {

    var r = random(0, 256), g = random(0, 256), b = random(0, 256);

    //模板字符串“ rgb{${r},${g},${b})

    var result = 'rgb(' + r + ',' + g + ',' + b + ')';

    return result;
}

//4位验证码 字母和数字
function randomnum(wnum) {

    var rcode = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

    var result = '';
    for (let i = 0; i < wnum; i++) {
        var j = random(0, 35);
        result += rcode[j];
    }
    return result;
}

//获取元素属性的函数
function getstyle(obj, attr) {
    if (obj.currentStyle) {
        //针对IE
        return obj.currentStyle[attr];
    } else {
        //针对火狐
        return getComputedStyle(obj, null)[attr];
    }
}

//动画函数
function widthAnimtion(obj, end, attr, fn) {
    var flag=true;//标杆  如果true,证明所有属性到达终点属性
    // 删除定时器
    clearInterval(obj.time);
    obj.time = setInterval(function () {
        //获取样式
        var w = 0;
        switch (attr) {
            case 'opacity':
                w = Math.round(parseFloat(getstyle(obj, attr)) * 100);
                break;
            case 'scrollTop':
                w = obj[attr];
                console.log(w);
                break;
            default:
                w = parseInt(getstyle(obj, attr));
                break;
        }
        //求速度
        speed = (end - w) / 20;
        speed = end > w ? Math.ceil(speed) : Math.floor(speed);
        //边界值处理
        if (w == end) {
            clearInterval(obj.time);
            if (fn) {
                fn();
            }
            return;
        }
        //运动起来
        switch (attr) {
            case 'opacity':
                obj.style.opacity = (w + speed) / 100;
                obj.style.opacity = "alpha(opacity:" + w + speed + ")";
                break;
            case 'scrollTop':
                obj.scrollTop = w + speed ;
                break;
            default:
                obj.style[attr] = w + speed + "px";
                break;
        }
    },60);
}

/*
        e = e || window.event;
        var target=e.target||e.srcElement;
        if (e.stopPropagation){
            e.stopPropagation();
        }else{
            e.cancelBubble=true;
        }

*/



